//
//  RCDiscountMainVC.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-6-29.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCDiscountMainVC.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "NSString+NSString_Ex_.h"
#import "GloableEngineService.h"
#import "RCMerchantDetailVC.h"
#import "RCTopListTypeDataModel.h"
#import "AppDelegate.h"
#import "NSArray+EX.h"
#import <QuartzCore/QuartzCore.h>


#define kHeaderViewFrame      CGRectMake(0,0,320,44)

#define kSignalImageFile               @"image_discount_signal.png"
#define kHeaderLineImageFile           @"image_line_type3.png"
#define kHeaderButtonPressedImageFile  @"image_discout_headerButton_pressed.png"
#define kHeaderTitleImageFile          @"image_topList_headerView_button.png"

#define kMinLatitudeOffset             0.001
#define kMinLongitudeOffset            0.002

@interface RCDiscountMainVC ()

@property(nonatomic,retain) UIButton *headerTitleButton;
@property(nonatomic,retain) UILabel *headerTitleLabel;
@property(nonatomic,retain) UIView *headerView;
@property(nonatomic,retain) UIButton *hotButton;
@property(nonatomic,retain) UILabel *hotLabel;
@property(nonatomic,retain) UIButton *bestButton;
@property(nonatomic,retain) UILabel *bestLabel;
@property(nonatomic,retain) UIButton *serviceButton;
@property(nonatomic,retain) UILabel *serviceLabel;
@property(nonatomic,retain) UIButton *environmentButton;
@property(nonatomic,retain) UILabel *environmentLabel;
@property(nonatomic,retain) UIView *subView;
@property(nonatomic,assign) TopListType pageType;
@property(nonatomic,retain) MBProgressHUDEx *progressHub;
@property(nonatomic,retain) RCDiscountSubView *hotSubView;
@property(nonatomic,retain) RCDiscountSubView *bestSubView;
@property(nonatomic,retain) RCDiscountSubView *serviceSubView;
@property(nonatomic,retain) RCDiscountSubView *environmentSubView;

@property(nonatomic,retain) NSMutableArray *tagButtonArray;
@property(nonatomic,retain) NSMutableArray *tagLabelArray;
@property(nonatomic,assign) int64_t typeRequetID;
@property(nonatomic,retain) RCTopListTypeDataModel *typeDataModel;
@property(nonatomic,retain) RCTopListTypeCategoryItem *currentCategoryItem;
@property(nonatomic,retain) RCToplistTypeView *typeListView;
@property(nonatomic,retain) UIButton *transparentButton;

@end

@implementation RCDiscountMainVC

@synthesize headerTitleButton = _headerTitleButton;
@synthesize headerTitleLabel = _headerTitleLabel;
@synthesize headerView = _headerView;
@synthesize hotButton = _hotButton;
@synthesize hotLabel = _hotLabel;
@synthesize bestButton = _bestButton;
@synthesize bestLabel = _bestLabel;
@synthesize serviceButton = _serviceButton;
@synthesize serviceLabel = _serviceLabel;
@synthesize environmentButton = _environmentButton;
@synthesize environmentLabel = _environmentLabel;
@synthesize subView = _subView;
@synthesize pageType = _pageType;
@synthesize currentLocation = _currentLocation;
@synthesize progressHub = _progressHub;
@synthesize hotSubView = _hotSubView;
@synthesize bestSubView = _bestSubView;
@synthesize serviceSubView = _serviceSubView;
@synthesize environmentSubView = _environmentSubView;

@synthesize tagButtonArray = _tagButtonArray;
@synthesize tagLabelArray = _tagLabelArray;
@synthesize typeRequetID = _typeRequetID;
@synthesize typeDataModel = _typeDataModel;
@synthesize currentCategoryItem = _currentCategoryItem;
@synthesize typeListView = _typeListView;
@synthesize transparentButton = _transparentButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_typeRequetID];
}

-(void)dealloc{
    
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_typeRequetID];
    self.headerTitleLabel = nil;
    self.headerTitleButton = nil;
    self.hotButton = nil;
    self.hotLabel = nil;
    self.bestButton = nil;
    self.bestLabel = nil;
    self.serviceButton = nil;
    self.serviceLabel = nil;
    self.environmentButton = nil;
    self.environmentLabel = nil;
    self.headerView = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.hotSubView = nil;
    self.bestSubView = nil;
    self.serviceSubView = nil;
    self.environmentSubView = nil;
    self.subView = nil;
    self.tagButtonArray = nil;
    self.tagLabelArray = nil;
    self.typeDataModel = nil;
    self.currentCategoryItem = nil;
    self.currentLocation = nil;
    self.typeListView = nil;
    self.transparentButton = nil;
    
    [super dealloc];
}

-(void)loadView{
    [super loadView];
    self.pageType = TopListTypeHot;
    [self createTitleButtonView];
    
}

-(void)createTitleButtonView{
    _headerTitleButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 157, 36)];
    _headerTitleButton.backgroundColor = [UIColor clearColor];
    [_headerTitleButton addTarget:self action:@selector(headerTitleButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_headerTitleButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kHeaderTitleImageFile ofType:nil]] forState:UIControlStateNormal];
    
    _headerTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 90, 16)];
    _headerTitleLabel.backgroundColor = [UIColor clearColor];
    _headerTitleLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    _headerTitleLabel.textColor = [UIColor whiteColor];
    [_headerTitleButton addSubview:_headerTitleLabel];
    
    self.navigationItem.titleView = _headerTitleButton;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _tagButtonArray = [[NSMutableArray alloc]initWithCapacity:4];
    _tagLabelArray = [[NSMutableArray alloc]initWithCapacity:4];

    _headerView = [[UIView alloc]initWithFrame:kHeaderViewFrame];
    _headerView.backgroundColor = [UIColor colorWithHex:0xffdcdcdc];
    
    _hotButton = [[UIButton alloc]initWithFrame:CGRectMake(14, 8, 62, 28)];
    [_hotButton setBackgroundImage:nil forState:UIControlStateNormal];
    [_hotButton setBackgroundColor:[UIColor clearColor]];
    _hotButton.tag = TopListTypeHot+100;
    _hotButton.layer.masksToBounds = YES;
    _hotButton.layer.cornerRadius = 3.0f;
    [_hotButton addTarget:self action:@selector(switchButtonCategory:) forControlEvents:UIControlEventTouchUpInside];
    
    _hotLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 7, 62, 14)];
    _hotLabel.backgroundColor = [UIColor clearColor];
    _hotLabel.textColor = [UIColor colorWithHex:0xff505050];
    _hotLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _hotLabel.textAlignment = UITextAlignmentCenter;
    [_hotButton addSubview:_hotLabel];
    [_headerView addSubview:_hotButton];
    
    [_tagButtonArray addObject:_hotButton];
    [_tagLabelArray addObject:_hotLabel];
    
    _bestButton = [[UIButton alloc]initWithFrame:CGRectMake(90, 8, 62, 28)];
    [_bestButton setBackgroundImage:nil forState:UIControlStateNormal];
    [_bestButton setBackgroundColor:[UIColor clearColor]];
    [_bestButton addTarget:self action:@selector(switchButtonCategory:) forControlEvents:UIControlEventTouchUpInside];
    _bestButton.tag = TopListTypeBest+100;
    _bestButton.layer.masksToBounds = YES;
    _bestButton.layer.cornerRadius = 3.0f;
    
    _bestLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 7, 62, 14)];
    _bestLabel.backgroundColor = [UIColor clearColor];
    _bestLabel.textColor = [UIColor colorWithHex:0xff505050];
    _bestLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _bestLabel.textAlignment = UITextAlignmentCenter;

    [_bestButton addSubview:_bestLabel];
    [_headerView addSubview:_bestButton];
    
    [_tagButtonArray addObject:_bestButton];
    [_tagLabelArray addObject:_bestLabel];
    
    _serviceButton = [[UIButton alloc]initWithFrame:CGRectMake(166, 8, 62, 28)];
    [_serviceButton setBackgroundImage:nil forState:UIControlStateNormal];
    [_serviceButton setBackgroundColor:[UIColor clearColor]];
    [_serviceButton addTarget:self action:@selector(switchButtonCategory:) forControlEvents:UIControlEventTouchUpInside];
    _serviceButton.tag = TopListTypeService +100;
    _serviceButton.layer.masksToBounds = YES;
    _serviceButton.layer.cornerRadius = 3.0f;
    
    _serviceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 7, 62, 14)];
    _serviceLabel.backgroundColor = [UIColor clearColor];
    _serviceLabel.textColor = [UIColor colorWithHex:0xff505050];
    _serviceLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _serviceLabel.textAlignment = UITextAlignmentCenter;
    [_serviceButton addSubview:_serviceLabel];
    [_headerView addSubview:_serviceButton];
    
    [_tagButtonArray addObject:_serviceButton];
    [_tagLabelArray addObject:_serviceLabel];
    
    _environmentButton = [[UIButton alloc]initWithFrame:CGRectMake(242, 8, 62, 28)];
    [_environmentButton setBackgroundImage:nil forState:UIControlStateNormal];
    [_environmentButton setBackgroundColor:[UIColor clearColor]];
    [_environmentButton addTarget:self action:@selector(switchButtonCategory:) forControlEvents:UIControlEventTouchUpInside];
    _environmentButton.tag = TopListTypeEnvironment +100;
    _environmentButton.layer.masksToBounds = YES;
    _environmentButton.layer.cornerRadius = 3.0f;
    
    _environmentLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 7, 62, 14)];
    _environmentLabel.backgroundColor = [UIColor clearColor];
    _environmentLabel.textColor = [UIColor colorWithHex:0xff505050];
    _environmentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _environmentLabel.textAlignment = UITextAlignmentCenter;
    [_environmentButton addSubview:_environmentLabel];
    [_headerView addSubview:_environmentButton];
    
    [_tagButtonArray addObject:_environmentButton];
    [_tagLabelArray addObject:_environmentLabel];
    
    UIImageView *headlineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 43, 320, 1)];
    headlineImageView.backgroundColor = [UIColor clearColor];
    [headlineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kHeaderLineImageFile ofType:nil]]];
    [_headerView addSubview:headlineImageView];
    [headlineImageView release];
    
    [self.viewContainer addSubview:_headerView];
    [self.viewContainer setMultipleTouchEnabled:NO];
    
    if (!_subView) {
        _subView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, 320, UIScreenHeight-20-44-44)];
        _subView.backgroundColor = [UIColor clearColor];
    }
    [self.viewContainer addSubview:_subView];
    
    if (!_progressHub)
    {
        _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view TopHeight:self.subView.frame.origin.y withBottomHeight:0 withLeftWidth:0 withRightWidth:0 withExclusiveObjects:nil];
        [self.view addSubview:_progressHub];
    }
    [_progressHub setFrameWithView:self.view
                         TopHeight:self.subView.frame.origin.y
                  withBottomHeight:0
                     withLeftWidth:0
                    withRightWidth:0];
    [self.view bringSubviewToFront:_progressHub];
    _progressHub.delegate = self;
    _progressHub.labelText= @"加载中...";
    [_progressHub show:YES];
    
    if ([GloableEngineService sharedInstance].locationInfo) {
        [_currentLocation release];
        _currentLocation = nil;
        self.currentLocation = [GloableEngineService sharedInstance].locationInfo;
    }
    // current select button is scene
//    [self switchButtonCategory:_eatButton];
    
    _typeRequetID = [[IManager sharedInstance]requestTopListTypeInfo:self];
    
	// Do any additional setup after loading the view.
}

-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_typeRequetID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        RCTopListTypeDataModel *typeDataModel = (RCTopListTypeDataModel *)dataModel;
        self.typeDataModel = typeDataModel;
        [self refreshHeaderButtonWithCategoryID:nil];
        _typeListView = [[RCToplistTypeView alloc]initWithFrame:CGRectMake(0, 0, 320, 260) andDataSource:self.typeDataModel];
        _typeListView.delegate = self;
        _typeListView.hidden = YES;
        [self.viewContainer addSubview:_typeListView];
        _transparentButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 260, 320, UIScreenHeight-64-260)];
        
        _transparentButton.backgroundColor = [UIColor colorWithHex:0x77000000];
        [_transparentButton addTarget:self action:@selector(transparentButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        _transparentButton.hidden = YES;
        [self.viewContainer addSubview:_transparentButton];
    }
}

-(void)updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId{
    if (_typeRequetID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        [RCAppDelegate alertWithCustomizeString:@"加载失败，请稍后重试"];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)headerTitleButtonClicked{
    if (!_typeListView) {
        return;
    }
    if (_typeListView.hidden) {
        _typeListView.hidden = NO;
        _transparentButton.hidden = NO;
        [self.viewContainer bringSubviewToFront:_typeListView];
        [self.viewContainer bringSubviewToFront:_transparentButton];
    }else{
        _typeListView.hidden = YES;
        _transparentButton.hidden = YES;
    }
}

-(void)transparentButtonClicked{
    _typeListView.hidden = YES;
    _transparentButton.hidden = YES;
}

#pragma -mark ToplistTypeViewDelegate

-(void)selectToplistTypeItem:(RCTopListTypeCategoryItem *)item{
    _typeListView.hidden = YES;
    _transparentButton.hidden = YES;
    if (nil == item) {
        return;
    }
    [self refreshHeaderButtonWithCategoryID:item];
}



-(void)refreshHeaderButtonWithCategoryID:(RCTopListTypeCategoryItem *)categoryItem{
    
    if (nil == categoryItem) {
        categoryItem = [self.typeDataModel.categoryListArray safeObjectAtIndex:0];
        _pageType = TopListTypeHot;
        
        
    }else{
        if ([categoryItem.categoryID isEqualToString:self.currentCategoryItem.categoryID] ) {
            return;
        }
        
    }
    
    if (nil == categoryItem) {
        return;
    }
    
    for (UIButton *button in _tagButtonArray) {
        button.hidden = YES;
    }
    _headerTitleLabel.text = categoryItem.categoryName;
    self.currentCategoryItem = categoryItem;
    NSArray *typeItemArray = categoryItem.typeItemArray;
    for (int i=0; i < [typeItemArray count]; i++) {
        RCTopListTypeItem *typeItem = [typeItemArray safeObjectAtIndex:i];
        if (nil == typeItem) {
            continue;
        }
        
        UIButton *button = [_tagButtonArray safeObjectAtIndex:i];
        if (button) {
            button.hidden = NO;
        }
        
        UILabel *label = [_tagLabelArray safeObjectAtIndex:i];
        if (label) {
            label.text = typeItem.typeName;
        }
    }
    
    switch (self.pageType) {
        case TopListTypeHot:
            [self switchButtonCategory:_hotButton];
            break;
        case TopListTypeBest:
            [self switchButtonCategory:_bestButton];
            break;
        case TopListTypeService:
            [self switchButtonCategory:_serviceButton];
            break;
        case TopListTypeEnvironment:
            [self switchButtonCategory:_environmentButton];
            break;
        default:
            break;
    }
    
}


-(void)updateLocationInfo:(CLLocation *)changeLocation{
    if (nil == changeLocation) {
        return;
    }
    if (fabs(_currentLocation.coordinate.latitude - changeLocation.coordinate.latitude) >= kMinLatitudeOffset ||
        fabs(_currentLocation.coordinate.longitude - changeLocation.coordinate.longitude) >= kMinLongitudeOffset)
    {
        if (![_currentLocation isEqual:changeLocation])
        {
            [_currentLocation release];
            _currentLocation = nil;
            
            self.currentLocation = changeLocation;
        }
    }
}

-(void)createHotSubView{
    if (!_hotSubView) {
        
        NSString *typeID = nil;
        RCTopListTypeItem *typeItem = [self.currentCategoryItem.typeItemArray safeObjectAtIndex:0];
        typeID = typeItem.typeID;
        
        _hotSubView = [[RCDiscountSubView alloc]initWithFrame:self.subView.bounds andDelegate:self andType:typeID andTypeName:typeItem.typeName andLocation:self.currentLocation];
        _hotSubView.delegate = self;
        _hotSubView.backgroundColor = [UIColor clearColor];
        _hotSubView.baseTableView.backgroundColor = [UIColor clearColor];
        _hotSubView.hidden = YES;
        [self.subView addSubview:_hotSubView];
        _hotSubView.baseTableView.frame = self.subView.bounds;
    }
}
-(void)createBestSubView{
    if (!_bestSubView) {
        
        NSString *typeID = nil;
        RCTopListTypeItem *typeItem = [self.currentCategoryItem.typeItemArray safeObjectAtIndex:1];
        typeID = typeItem.typeID;
        
        _bestSubView = [[RCDiscountSubView alloc]initWithFrame:self.subView.bounds andDelegate:self andType:typeID andTypeName:typeItem.typeName andLocation:self.currentLocation];
        _bestSubView.delegate = self;
        _bestSubView.backgroundColor = [UIColor clearColor];
        _bestSubView.baseTableView.backgroundColor = [UIColor clearColor];
        _bestSubView.hidden = YES;
        [self.subView addSubview:_bestSubView];
        _bestSubView.baseTableView.frame = self.subView.bounds;
    }
}
-(void)createServiceSubView{
    if (!_serviceSubView) {
        
        NSString *typeID = nil;
        RCTopListTypeItem *typeItem = [self.currentCategoryItem.typeItemArray safeObjectAtIndex:2];
        typeID = typeItem.typeID;
        
        _serviceSubView = [[RCDiscountSubView alloc]initWithFrame:self.subView.bounds andDelegate:self andType:typeID andTypeName:typeItem.typeName andLocation:self.currentLocation];
        _serviceSubView.delegate = self;
        _serviceSubView.backgroundColor = [UIColor clearColor];
        _serviceSubView.baseTableView.backgroundColor = [UIColor clearColor];
        _serviceSubView.hidden = YES;
        [self.subView addSubview:_serviceSubView];
        _serviceSubView.baseTableView.frame = self.subView.bounds;
    }
}
-(void)createEnvironmentSubView{
    if (!_environmentSubView) {
        
        NSString *typeID = nil;
        RCTopListTypeItem *typeItem = [self.currentCategoryItem.typeItemArray safeObjectAtIndex:3];
        typeID = typeItem.typeID;
        
        
        _environmentSubView = [[RCDiscountSubView alloc]initWithFrame:self.subView.bounds andDelegate:self andType:typeID andTypeName:typeItem.typeName andLocation:self.currentLocation];
        _environmentSubView.delegate = self;
        _environmentSubView.backgroundColor = [UIColor clearColor];
        _environmentSubView.baseTableView.backgroundColor = [UIColor clearColor];
        _environmentSubView.hidden = YES;
        [self.subView addSubview:_environmentSubView];
        _environmentSubView.baseTableView.frame = self.subView.bounds;
    }
}


-(void)switchButtonCategory:(id)sender{
    UIButton *button = (UIButton *)sender;
    NSInteger tagNum = button.tag;
    TopListType type = tagNum-100;
    
    
    switch (type) {
        case TopListTypeHot:{
            
            NSString *typeID = nil;
            RCTopListTypeItem *typeItem = [self.currentCategoryItem.typeItemArray safeObjectAtIndex:0];
            typeID = typeItem.typeID;
            
            [_hotButton setBackgroundColor:[UIColor colorWithHex:0xffbdbbbb]];
            [_bestButton setBackgroundColor:[UIColor clearColor]];
            [_serviceButton setBackgroundColor:[UIColor clearColor]];
            [_environmentButton setBackgroundColor:[UIColor clearColor]];
            
            [self createHotSubView];
            _hotSubView.hidden = NO;
            if (_bestSubView) {
                _bestSubView.hidden = YES;
            }
            if (_serviceSubView) {
                _serviceSubView.hidden = YES;
            }
            if (_environmentSubView) {
                _environmentSubView.hidden = YES;
            }
            

            if (fabs(_currentLocation.coordinate.latitude - _hotSubView.location.coordinate.latitude) >= kMinLatitudeOffset ||
                fabs(_currentLocation.coordinate.longitude - _hotSubView.location.coordinate.longitude) >= kMinLongitudeOffset)
            {
                if (![_currentLocation isEqual:_hotSubView.location])
                {
                    _hotSubView.location = nil;
                    
                    _hotSubView.location = self.currentLocation;
                    [_hotSubView.itemListArray removeAllObjects];
                    [_hotSubView reloadData];
                    _hotSubView.currentOffset = 0;
                    _hotSubView.currentReturnNumber = 0;
                    [_hotSubView requestDataList:YES];
                }
            }else if (![_hotSubView.typeID isEqualToString:typeID]){
                _hotSubView.typeID = typeID;
                [_hotSubView.itemListArray removeAllObjects];
                [_hotSubView reloadData];
                _hotSubView.currentOffset = 0;
                _hotSubView.currentReturnNumber = 0;
                [_hotSubView requestDataList:YES];
                
            }else{
                [_hotSubView reloadData];
            
            }
            [self.subView bringSubviewToFront:_hotSubView];
            
            break;
        }
        case TopListTypeBest:{
            
            NSString *typeID = nil;
            RCTopListTypeItem *typeItem = [self.currentCategoryItem.typeItemArray safeObjectAtIndex:1];
            typeID = typeItem.typeID;
            
            [_hotButton setBackgroundColor:[UIColor clearColor]];
            [_bestButton setBackgroundColor:[UIColor colorWithHex:0xffbdbbbb]];
            [_serviceButton setBackgroundColor:[UIColor clearColor]];
            [_environmentButton setBackgroundColor:[UIColor clearColor]];
            
            [self createBestSubView];
            _bestSubView.hidden = NO;
            if (_hotSubView) {
                _hotSubView.hidden = YES;
            }
            if (_serviceSubView) {
                _serviceSubView.hidden = YES;
            }
            if (_environmentSubView) {
                _environmentSubView.hidden = YES;
            }
            
            if (fabs(_currentLocation.coordinate.latitude - _bestSubView.location.coordinate.latitude) >= kMinLatitudeOffset ||
                fabs(_currentLocation.coordinate.longitude - _bestSubView.location.coordinate.longitude) >= kMinLongitudeOffset)
            {
                if (![_currentLocation isEqual:_bestSubView.location])
                {
                    _bestSubView.location = nil;
                    
                    _bestSubView.location = self.currentLocation;
                    [_bestSubView.itemListArray removeAllObjects];
                    [_bestSubView reloadData];
                    _bestSubView.currentOffset = 0;
                    _bestSubView.currentReturnNumber = 0;
                    [_bestSubView requestDataList:YES];
                }
            }else if (![_bestSubView.typeID isEqualToString:typeID]){
                _bestSubView.typeID = typeID;
                [_bestSubView.itemListArray removeAllObjects];
                [_bestSubView reloadData];
                _bestSubView.currentOffset = 0;
                _bestSubView.currentReturnNumber = 0;
                [_bestSubView requestDataList:YES];
                
            }else{
                [_bestSubView reloadData];
                
            }
            [self.subView bringSubviewToFront:_bestSubView];
            break;
        }
        case TopListTypeService:{
            
            NSString *typeID = nil;
            RCTopListTypeItem *typeItem = [self.currentCategoryItem.typeItemArray safeObjectAtIndex:2];
            typeID = typeItem.typeID;
            
            [_hotButton setBackgroundColor:[UIColor clearColor]];
            [_bestButton setBackgroundColor:[UIColor clearColor]];
            [_serviceButton setBackgroundColor:[UIColor colorWithHex:0xffbdbbbb]];
            [_environmentButton setBackgroundColor:[UIColor clearColor]];
            
            [self createServiceSubView];
            _serviceSubView.hidden = NO;
            if (_hotSubView) {
                _hotSubView.hidden = YES;
            }
            if (_bestSubView) {
                _bestSubView.hidden = YES;
            }
            if (_environmentSubView) {
                _environmentSubView.hidden = YES;
            }
            
            
            if (fabs(_currentLocation.coordinate.latitude - _serviceSubView.location.coordinate.latitude) >= kMinLatitudeOffset ||
                fabs(_currentLocation.coordinate.longitude - _serviceSubView.location.coordinate.longitude) >= kMinLongitudeOffset)
            {
                if (![_currentLocation isEqual:_serviceSubView.location])
                {
                    _serviceSubView.location = nil;
                    
                    _serviceSubView.location = self.currentLocation;
                    [_serviceSubView.itemListArray removeAllObjects];
                    [_serviceSubView reloadData];
                    _serviceSubView.currentOffset = 0;
                    _serviceSubView.currentReturnNumber = 0;
                    [_serviceSubView requestDataList:YES];
                }
            }else if (![_serviceSubView.typeID isEqualToString:typeID]){
                _serviceSubView.typeID = typeID;
                [_serviceSubView.itemListArray removeAllObjects];
                [_serviceSubView reloadData];
                _serviceSubView.currentOffset = 0;
                _serviceSubView.currentReturnNumber = 0;
                [_serviceSubView requestDataList:YES];
                
            }else{
                [_serviceSubView reloadData];
                
            }
            [self.subView bringSubviewToFront:_serviceSubView];
            break;
        }
        case TopListTypeEnvironment:{
            
            NSString *typeID = nil;
            RCTopListTypeItem *typeItem = [self.currentCategoryItem.typeItemArray safeObjectAtIndex:3];
            typeID = typeItem.typeID;
            
            [_hotButton setBackgroundColor:[UIColor clearColor]];
            [_bestButton setBackgroundColor:[UIColor clearColor]];
            [_serviceButton setBackgroundColor:[UIColor clearColor]];
            [_environmentButton setBackgroundColor:[UIColor colorWithHex:0xffbdbbbb]];
            
            [self createEnvironmentSubView];
            _environmentSubView.hidden = NO;
            if (_hotSubView) {
                _hotSubView.hidden = YES;
            }
            if (_bestSubView) {
                _bestSubView.hidden = YES;
            }
            if (_serviceSubView) {
                _serviceSubView.hidden = YES;
            }
            
            if (fabs(_currentLocation.coordinate.latitude - _environmentSubView.location.coordinate.latitude) >= kMinLatitudeOffset ||
                fabs(_currentLocation.coordinate.longitude - _environmentSubView.location.coordinate.longitude) >= kMinLongitudeOffset)
            {
                if (![_currentLocation isEqual:_environmentSubView.location])
                {
                    _environmentSubView.location = nil;
                    
                    _environmentSubView.location = self.currentLocation;
                    [_environmentSubView.itemListArray removeAllObjects];
                    [_environmentSubView reloadData];
                    _environmentSubView.currentOffset = 0;
                    _environmentSubView.currentReturnNumber = 0;
                    if (!_environmentSubView.isMoreTableFont) {
                        [_environmentSubView requestDataList:YES];
                    }
                    
                }
            }else if (![_environmentSubView.typeID isEqualToString:typeID] && ![_environmentSubView.moreTypeID isEqualToString:typeID]){
                _environmentSubView.moreTypeID = typeID;
                _environmentSubView.typeID = typeID;
                [_environmentSubView.itemListArray removeAllObjects];
                [_environmentSubView reloadData];
                _environmentSubView.currentOffset = 0;
                _environmentSubView.currentReturnNumber = 0;
                [_environmentSubView requestMoreTypeData];
                
            }else{
                if (self.pageType == TopListTypeEnvironment) {
                    [_environmentSubView switchTabbarButtonClicked];
                }else if (self.pageType != TopListTypeEnvironment && !_environmentSubView.isMoreTableFont){
                    [_environmentSubView reloadData];
                }
                
                
            }
            [self.subView bringSubviewToFront:_environmentSubView];
            break;
        }
        
        
        default:
            break;
    }
    
    self.pageType = type;
    
}

#pragma -DiscountSubViewDelegate

- (void)beginRequestData
{
    if (!_progressHub)
    {
        _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view TopHeight:self.subView.frame.origin.y withBottomHeight:0 withLeftWidth:0 withRightWidth:0 withExclusiveObjects:nil];
        [self.view addSubview:_progressHub];
    }
    [_progressHub setFrameWithView:self.view
                         TopHeight:self.subView.frame.origin.y
                  withBottomHeight:0
                     withLeftWidth:0
                    withRightWidth:0];
    [self.view bringSubviewToFront:_progressHub];
    _progressHub.delegate = self;
    _progressHub.labelText= @"加载中...";
    [_progressHub show:YES];
}

- (void)endRequestData
{
    [_progressHub hide:NO afterDelay:1.0];
}

-(void)gotoDiscountChildView:(RCMerchant *)itemInfo WithChildViewType:(DiscountType)subViewType{
    if (nil == itemInfo && [NSString stringIsNullOrEmpty:itemInfo.merchantID]) {
        return;
    }
    RCMerchantDetailVC *vc = [[RCMerchantDetailVC alloc]initWithMerchantID:itemInfo.merchantID];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
